package com.client;

import com.model.Student;
import com.model.User;
import com.model.UserMessage;

import java.io.IOException;
import java.util.HashMap;

/**
 * 用户模块子系统
 */
public class StudentSubSystemView {
    private ClientInitClose clientInitClose;

    public StudentSubSystemView() {
    }

    public StudentSubSystemView(ClientInitClose clientInitClose) {
        this.clientInitClose = clientInitClose;
    }

    /**
     * 用户模块子界面
     */
    public void studentSubSystemMenu(UserMessage userMessage) throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("------------------------------");
            System.out.println("[1] 修改密码         [0] 退出");
            System.out.println("------------------------------");
            System.out.println("请选择您要操作的模块：");
            int option = ClientScanner.getScanner().nextInt();

            switch (option){
                case 1:
                    //System.out.println("正在修改密码！");
                    // 客户端定义修改密码
                    System.out.println("请输入学员原始密码：");
                    String oldPassWord = ClientScanner.getScanner().next();
                    System.out.println("请输入用户新的密码：");
                    String newPassWord = ClientScanner.getScanner().next();
                    // 修改密码
                    if (oldPassWord.equals(userMessage.getUser().getPassWord())){
                        // 先定义用户修改密码标示
                        userMessage.setMessage("modifyPassWord");
                        userMessage.getUser().setPassWord(newPassWord);
                        // 发送给服务器
                        clientInitClose.getObjectOutputStream().writeObject(userMessage);
                        clientInitClose.getObjectOutputStream().flush();
                        // 从服务器接收
                        UserMessage receiveUserMessage = (UserMessage) clientInitClose.getObjectInputStream().readObject();
                        if (receiveUserMessage.getMessage().equals("successModifyPassWord"))
                            System.out.println("学员密码修改成功！");
                        else
                            System.out.println("学员密码修改失败！");
                    }else
                        System.out.println("学员原始密码输入错误！");
                    break;
                case 0:
                    System.out.println("正在退出学员系统。。。。。。");
                    clientInitClose.getObjectOutputStream().writeObject(new UserMessage("quit",null));
                    clientInitClose.getObjectOutputStream().flush();
                    return;
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }
}
